(function() {
  app.directive('centerDilog', function() {
    return {
      restrict: 'E',
      templateUrl: 'public/directive/center-dilog.html',
      replace: true,
      scope: {},
      controller: function($scope, centerdilog, $rootScope, $compile) {
        $scope.ctrl = centerdilog;
      },
      link: function(scope, ele, atr) {

      }

    };
  });

  app.factory('centerdilog', function($timeout, $sce, $rootScope, $compile) {
    return {
      view: false,
      open: function(obj) {
        this.view = true;
        var that = this;
        this.closetxt = obj.closetxt || '取消';
        this.oktxt = obj.oktxt || '确定';
        this.msg = obj.msg || '未知';
        this.title = obj.title || '提示';
        this.ok_show = 'ok_hiden' in obj ? false : true; // 参数带有ok_hiden/cancel_hiden 隐藏按钮
        this.cancel_show = 'cancel_hiden' in obj ? false : true;

        this.tpl_show = obj.tpl || false;
        this.template = $sce.trustAsHtml('');
        this.tpl_scope = obj.tpl_scope || ''; // 传的参数
        this.template_scope = null; // 当前模板的作用域

        if (obj.tpl) {
          if (this.tpl_scope) {
            this.template_scope = $rootScope.$new();
            angular.extend(that.template_scope, this.tpl_scope);
            var tpl = angular.element(obj.tpl);
            angular.element(document).ready(function() {
              angular.element('#center-dilog-content-template').append($compile(tpl)(that.template_scope));
            });
          } else {
            this.template = $sce.trustAsHtml(obj.tpl);
            angular.element(document).ready(function() {
              angular.isFunction(obj.tpl_cb) ? obj.tpl_cb() : '';
            });
          }
        }
        if (obj.cancel) {
          this.cancel = function() {
            angular.isFunction(obj.cancel) ? obj.cancel() : '';
            that.view = false;
          };
        } else {
          this.cancel = function() {
            that.view = false;
          };
        }
        if (obj.ok) {
          this.ok = function() {
            angular.isFunction(obj.ok) ? obj.ok() : '';
            that.view = false;
          };
        } else {
          this.ok = function() {
            that.view = false;
          };
        }
      }

    };
  });
})();